package com.itheima.mall.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mall.exception.BusinessException;
import com.itheima.mall.exception.ErrorResult;
import com.itheima.mall.mapper.HomeNewProductMapper;
import com.itheima.mall.pojo.HomeNewProduct;
import com.itheima.mall.service.SmsHomeNewProductService;
import com.itheima.mall.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SmsHomeNewProductServiceImpl extends ServiceImpl<HomeNewProductMapper, HomeNewProduct> implements SmsHomeNewProductService {

    @Autowired
    private HomeNewProductMapper homeNewProductMapper;


    /**
     * 分页查询首页新品
     */
    @Override
    public PageResult getList(Integer pageNum, Integer pageSize, Integer productName, Integer recommendStatus) {
        Page<HomeNewProduct> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<HomeNewProduct> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(productName != null,HomeNewProduct::getProductName,productName)
                .eq(recommendStatus != null,HomeNewProduct::getRecommendStatus,recommendStatus)
                .orderByAsc(HomeNewProduct::getSort);
        Page<HomeNewProduct> pageInfo = homeNewProductMapper.selectPage(page, queryWrapper);
        return new PageResult(pageNum,pageSize, (int) pageInfo.getTotal(),pageInfo.getRecords());
    }

    /**
     * 添加首页新品
     */
    @Override
    public void createNew(List<HomeNewProduct> homeNewProductList) {
        if (CollUtil.isEmpty(homeNewProductList)){
            throw new BusinessException(ErrorResult.error());
        }
        homeNewProductList = homeNewProductList.stream().map(itme -> {
            itme.setSort(100);
            return itme;
        }).collect(Collectors.toList());
        this.saveBatch(homeNewProductList);
    }

    /**
     * 批量删除首页新品
     */
    @Override
    public void deleteNew(List<Long> ids) {
        if (CollUtil.isEmpty(ids)){
            throw new BusinessException(ErrorResult.error());
        }
        this.removeBatchByIds(ids);
    }

    /**
     * 批量修改首页新品状态
     */
    @Override
    public void updateStatus(List<Long> ids, Integer recommendStatus) {
        LambdaUpdateWrapper<HomeNewProduct> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(CollUtil.isNotEmpty(ids),HomeNewProduct::getId,ids)
                .set(recommendStatus != null,HomeNewProduct::getRecommendStatus,recommendStatus);
        homeNewProductMapper.update(null,updateWrapper);
    }

    /**
     * 修改首页新品排序
     */
    @Override
    public void updateSort(Long id, Integer sort) {
        LambdaUpdateWrapper<HomeNewProduct> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(HomeNewProduct::getId,id)
                .set(HomeNewProduct::getSort,sort);
        homeNewProductMapper.update(null,updateWrapper);
    }


}
